var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    // 用户id
    userId:0,
    useSealData:null,
    // 相机高度
    cameraHeight:500,
    // 底部高度
    footerHeight:80,
    // 上传的图片地址
    imageUrls:[],
    // 预览图片数组
    previewImageUrls:[],
    // 当前显示图片
    currentImageUrl:null,
    showPreview:false,
    navbarData:{
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '用印文件拍照(0)', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     navbarHeight: app.globalData.height * 2 + 20
  },
  onLoad(){
    var _this = this;
    // 读取用户id
    var userInfo = wx.getStorageSync('userInfo');
    _this.setData({
      userId:userInfo.id,
    });
    // 读取已填写的单据
    var useSealData = wx.getStorageSync('useSealData');
    _this.setData({
      useSealData:useSealData,
    });
    requestClient = new ZYRequestClient();
    // 设置相机高度，铺满整个屏幕
    wx.getSystemInfo({
      success: (result) => {
        _this.setData({
          cameraHeight:result.windowHeight - _this.data.navbarHeight - _this.data.footerHeight
        });
      },
    })
    // 检查摄像机权限
    wx.getSetting({
      success(res){
        var authSetting = res.authSetting;
        if(authSetting['scope.camera']){
        } else {
          wx.showModal({
            title:'获取权限',
            content:'请授权摄像机权限，否则无法进行文件拍照！',
            showCancel:false,
            success(r){
              
            }
          })
        }
      }
    })
  },
  onShow(){
  },
  // 执行拍照
  takePhoto() {
    var _this = this;
    wx.showLoading({
      title: '正在拍照上传...',
      mask:true,
    })
    const cameraContext = wx.createCameraContext();
    cameraContext.takePhoto({
      quality: 'normal',
      success: (res) => {
        // 上传照片
        requestClient.uploadImage(_this.data.userId,res.tempImagePath,function(res){
          wx.hideLoading({
            success: (res) => {},
          })
          if(res.data == null || res.data.code >1){
            wx.showModal({
              title:'上传失败',
              content:'图片上传失败，请重新拍照上传！',
              showCancel:false,
              success(r){

              }
            })
            return;
          }
          _this.setData({
            currentImageUrl:res.data,
          });
          var imageUrls = _this.data.imageUrls;
          imageUrls.push(res.data);
          _this.setData({
            imageUrls:imageUrls,
            previewImageUrls:imageUrls,
            'navbarData.title':'用印文件拍照(' + imageUrls.length + ')',
          });
        });
      }
    })
  },
  error(e) {
    wx.showToast({
      title: '摄像机拍照错误!',
      icon:'error'
    })
  },
  // 查看拍照图片
  bindGoScanPhoto(e){
    var _this = this;
    _this.setData({
      showPreview:true,
      previewImageUrls:_this.data.imageUrls,
    });
  },
  // 删除图片
  deleteImage(e){
    var _this = this;
    var index = e.detail.index;
    var imageUrls = _this.data.imageUrls;
    imageUrls.splice(index,1);
    _this.setData({
      imageUrls:imageUrls,
      'navbarData.title':'用印文件拍照(' + imageUrls.length + ')',
    });
  },
  // 隐藏图片显示
  hideImage(e){
    var _this = this;
    var imgLength = _this.data.imageUrls.length;
    if(imgLength == 0){
      _this.setData({
        currentImageUrl:null,
      })
    } else {
      // 赋值最近拍照的图片
      _this.setData({
        currentImageUrl:_this.data.imageUrls[imgLength - 1],
      })
    }
  },
  // 完成盖章拍照
  completePhoto(e){
    var _this = this;
    // 判断是否拍照上传
    if(_this.data.imageUrls.length == 0){
      wx.showToast({
        title: '请拍照上传!',
        icon:'none'
      })
      return;
    }
    wx.showLoading({
      title: '正在提交...',
      mask:true,
    })
    _this.setData({
      'useSealData.imageUrls':_this.data.imageUrls,
    });
    // 提交申请单
    requestClient.addUseSealApply(_this.data.useSealData,function(res){
      wx.hideLoading({
        success: (res) => {},
      })
      if(res.data.code == 1){
        // 询问用户是否订阅审批通知提醒
        wx.requestSubscribeMessage({
          tmplIds:['PhmLACtYXZlbGn2aUwcIPzpuuhygVupQvVlUQz0BSko','WTQYgj76ljMyEHDv_KAAQqlj69zDtKWW68lmKWZ1oy0'],
          success (res) { 
            wx.showModal({
              title:'用印申请',
              content:'单据已提交成功，正在进入审批流程，请耐心等待！',
              showCancel:false,
              success(r){
                if(r.confirm){
                  wx.switchTab({
                    url: '../../home/index',
                  })
                }
              }
            })
          }
        });
      } else {
        wx.showModal({
          title:'提交失败',
          content:res.data.msg,
          showCancel:false,
          success(r){
            if(r.confirm){
              wx.switchTab({
                url: '../../home/index',
              })
            }
          }
        })
      }
    })
  },
})